﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Text.RegularExpressions;
using System.Collections.ObjectModel;
using System.Xml.Serialization;
using System.Xml;

namespace Dota2WebAPI
{
    public class Server
    {
        private string steamAPI;

        public Server(string steamAPI)
        {
            this.steamAPI = steamAPI;
            GetHeroes();
        }

        public Server(string steamAPI, HeroList heroes, ItemList items)
        {

        }

        public HeroList GetHeroes()
        {
            HeroList result = null;
            try
            {
                string formatedURL = String.Format("https://api.steampowered.com/IEconDOTA2_570/GetHeroes/v0001/?key={0}&language=en_us&format=xml", this.steamAPI);

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(formatedURL);
                request.Method = WebRequestMethods.Http.Get;

                // Výsledek requestu
                var response = (HttpWebResponse)request.GetResponse();
                string text;

                XmlSerializer serialiser = new XmlSerializer(typeof(HeroList));
                XmlTextReader reader = new XmlTextReader(response.GetResponseStream());



                result = (HeroList)serialiser.Deserialize(reader);
                return result;

            }
            catch (InvalidOperationException e)
            {
                throw;
            }
        }

        public ItemList GetItems()
        {
            throw new NotImplementedException("Valve in charge of API");
        }

        public MatchDetails GetLatestMatch(Int64 user)
        {
            return null;
        }

        public MatchOverview GetMatchHistory(Int64 user)
        {
            return GetMatchHistory(user,10);
        }

        public MatchOverview GetMatchHistory(Int64 user, int matches)
        {
            if (user == null)
            {
                throw new NullReferenceException("User not set");
            }

            MatchOverview result = null;
            try
            {
                string formatedURL = String.Format("https://api.steampowered.com/IDOTA2Match_570/GetMatchHistory/V001/?format=XML&key={0}&account_id={1}", this.steamAPI, user);

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(formatedURL);
                request.Method = WebRequestMethods.Http.Get;

                // Výsledek requestu
                var response = (HttpWebResponse)request.GetResponse();

                XmlSerializer serialiser = new XmlSerializer(typeof(MatchOverview));
                XmlTextReader reader = new XmlTextReader(response.GetResponseStream());



                result = (MatchOverview)serialiser.Deserialize(reader);
                return result;

            }
            catch (InvalidOperationException e)
            {
                throw;
            }
        }
    }
}
